package com.epam.facebook_client;

import java.util.ArrayList;
import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.facebook.FacebookActivity;
import com.facebook.Session;
import com.facebook.widget.LoginButton;


public class LoginActivity extends FacebookActivity {

	private List<String> permissions;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_button);
		LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
		permissions = new ArrayList<String>();
		permissions.add("publish_actions");
		permissions.add("publish_stream");
		permissions.add("read_stream");
		permissions.add("user_photos");
		permissions.add("friends_photos");
		permissions.add("friends_about_me");
		permissions.add("friends_birthday");
		permissions.add("friends_education_history");
		permissions.add("friends_work_history");
		loginButton.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				openSessionForPublish(getString(R.string.app_id), permissions);
			}
		});

		
	}
	
	@Override
	protected void onResume() {
		super.onResume();	
		Session session = Session.getActiveSession();
	    if (session != null && session.getState().isOpened()) {
	    	String user = "Facebook Client";
			Account account = new Account(user , getApplication().getString(R.string.ACCOUNT_TYPE));
			AccountManager am = AccountManager.get(getApplication());
			String pass = "";
			if (am.addAccountExplicitly(account, pass, null)) {
				ContentResolver.setIsSyncable(account, "com.epam.facebook_client.content.FacebookProvider", 1);
				Bundle bundle = new Bundle();
				bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
				bundle.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
				bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
				ContentResolver.addPeriodicSync(account, "com.epam.facebook_client.content.FacebookProvider", bundle, 3 * 60 * 60);
				ContentResolver.setSyncAutomatically(account, "com.epam.facebook_client.content.FacebookProvider", true);
			}
	        startActivity(new Intent(this, StartActivity.class));
	        finish();
	    }
	}

}
